/*
    
    网站通用导航栏 JS 不依赖jquery 实现
    yjbao add 
    2013-05-10
    email: yjbao@techsylar.com

*/
var cnstockLoginstate = function(obj){
    if(!obj || typeof obj !== 'object') return;
    if(obj.status == 0) return;
    var loginstate = document.getElementById('J_login');
    var isloginstate = document.getElementById('J_is_login');
    
    loginstate.style.display = 'none';
    isloginstate.style.display = 'inline-block';
    
    isloginstate.innerHTML = obj.html;
};

var  cnstockLogin = function(){
    window.top.location.href="https://passport.cnstock.com/PassPortWeb/login.pp?redirect=" + encodeURIComponent(window.top.location.href);
};
var  cnstockRegister = function(){
    window.top.location.href="https://passport.cnstock.com/PassPortWeb/register.pp?redirect=" + encodeURIComponent(window.top.location.href);
};
var cnstockLogout = function(){
    window.top.location.href = "https://t.cnstock.com/" + "?mod=login&code=logout&redirect="+encodeURIComponent(window.top.location.href);
};

function addEventHandler(element, evtName, callback, useCapture) {        
    if (element.addEventListener) {
        element.addEventListener(evtName, callback, useCapture);
    } else {
        element.attachEvent('on' + evtName, callback);
    }
}

var cnstocknav = {
    search : document.getElementById('J_nav_search'),
    searchBd : document.getElementById('J_search_bd'),
    searchHd : document.getElementById('J_search_hd'),
    searchVal : document.getElementById('J_search_val'),
    searchInput : document.getElementById('nav_keywords'),
    searchInputChange : document.getElementById('J_search_input')
};

var cnstockSearchShow = function(){
    return cnstocknav.searchBd.style.display = 'block';
};

var cnstockSearchHide = function(){
    return cnstocknav.searchBd.style.display = 'none';
};

var csntockSearchVal = function(num){
    return cnstocknav.searchVal.value = num;
};


var cnstockGetChildElement = function(ele,tagName){
    if(!(ele && ele.nodeType && ele.nodeType === 1)) return;
    var child = ele.firstChild;
    var a = [];
    var child = ele.firstChild;
    if(tagName){
        while(child){
            if(child.nodeType == 1 && child.tagName.toLowerCase() == tagName.toLowerCase()){
                var newNode = child.firstChild;
                while(newNode){
                    if(newNode.nodeType == 1 && newNode.tagName.toLowerCase() == 'a'){
                        a.push(newNode);
                    }
                    newNode = newNode.nextSibling;
                }
            }
            child = child.nextSibling;
        }
    }
    return a;
};

var cnstocknavClick = function(e){
    var target = e.currentTarget || e.srcElement;
    var cid = target.getAttribute('data-cid');
    var txt = target.innerHTML;
    if(!cid) return;
    //console.log('3');
    if(cnstocknav.searchInputChange && txt == '\u884c\u60c5'){
        //console.log('1');
        cnstocknav.searchInputChange.style.display = 'inline';
        cnstocknav.searchInputChange.setAttribute('name','qt');
        cnstocknav.searchInput.style.display = 'none';
        cnstocknav.searchInputChange.setAttribute('name','searchChange');
    }else{
        //console.log('2');
        cnstocknav.searchInputChange.style.display = 'none';
        cnstocknav.searchInputChange.setAttribute('name','searchChange');
        cnstocknav.searchInput.style.display = 'inline';
        cnstocknav.searchInputChange.setAttribute('name','qt');
    }
    csntockSearchVal(cid);
    cnstocknav.searchHd.innerHTML = txt;
    cnstockSearchHide();
};

var cnstockChange = function(ele){
    var a = cnstockGetChildElement(cnstocknav.searchBd,'li');
    for(var i = 0 ; i < a.length ; i ++){
        addEventHandler(a[i],'click',cnstocknavClick);
    }
};

function showNavForum(){
    return document.getElementById('nav-forum-bd').style.display = 'block';
}
function hideNavForum(){
    return document.getElementById('nav-forum-bd').style.display = 'none';
}
function showNavProduct(){
    return document.getElementById('nav_product_bd').style.display = 'block';
}
function hideNavProduct(){
    return document.getElementById('nav_product_bd').style.display = 'none';
}
function showNavCommunity(){
    return document.getElementById('nav_community_bd').style.display = 'block';
}
function hideNavCommunity(){
    return document.getElementById('nav_community_bd').style.display = 'none';
}
function showNavCompany(){
    return document.getElementById('nav_company_bd').style.display = 'block';
}
function hideNavCompany(){
    return document.getElementById('nav_company_bd').style.display = 'none';
}
function showNavNews(){
    return document.getElementById('nav_news_bd').style.display = 'block';
}
function hideNavNews(){
    return document.getElementById('nav_news_bd').style.display = 'none';
}

var navItem = {
    forum : document.getElementById('J_nav_forum'),
    product : document.getElementById('J_nav_product'),
    community : document.getElementById('J_nav_community'),
    company : document.getElementById('J_nav_company'),
    news : document.getElementById('J_nav_news')
};

addEventHandler(cnstocknav.search,'mouseover',cnstockSearchShow);
addEventHandler(cnstocknav.search,'mouseout',cnstockSearchHide);

if(navItem.forum){
    addEventHandler(navItem.forum,'mouseover',showNavForum);
    addEventHandler(navItem.forum,'mouseout',hideNavForum);
}
if(navItem.product){
    addEventHandler(navItem.product,'mouseover',showNavProduct);
    addEventHandler(navItem.product,'mouseout',hideNavProduct);
}
if(navItem.community){
    addEventHandler(navItem.community,'mouseover',showNavCommunity);
    addEventHandler(navItem.community,'mouseout',hideNavCommunity);
}
if(navItem.company){
    
    addEventHandler(navItem.company,'mouseover',showNavCompany);
    addEventHandler(navItem.company,'mouseout',hideNavCompany);
}
if(navItem.news){    
    addEventHandler(navItem.news,'mouseover',showNavNews);
    addEventHandler(navItem.news,'mouseout',hideNavNews);
}

cnstockChange();

(function() {
	if( typeof( window.app ) === 'undefined' ){
		window.app = 'common';
	}
	
	var cnstock_state = document.createElement('script'); 
	var time = new Date();
	cnstock_state.type = 'text/javascript'; cnstock_state.async = true;
	cnstock_state.src = 'https://passport.cnstock.com/PassPortWeb/login_status.pp?cb=cnstockLoginstate&app='+window.app+'&timestamp=' + time.getTime();
	var s = document.getElementsByTagName('body')[0];
	s.appendChild(cnstock_state);
})();

